package com.dtda.edu.miaosha.controller;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.bo.OrderBo;
import com.dtda.edu.miaosha.common.exception.BaseExcpController;
import com.dtda.edu.miaosha.common.response.ResponseType;
import com.dtda.edu.miaosha.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 袁强
 * @data 2019/5/14 9:05
 * @Description:
 */
@RestController
@RequestMapping("/")
public class ManageOrderController extends BaseExcpController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/toSelOrder")
    public ModelAndView selOrder() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/order/showOrders");
        return modelAndView;
    }
    @RequestMapping("/showOrders")
    @ResponseBody
    public ResponseType showOrders(){
        List<OrderBo> orders = orderService.showOrders();
        if(orders.size() == 0) {
            return ResponseType.create("fail", "没有订单!");
        }
        return ResponseType.create(orders);
    }
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public ResponseType deleteOrder(@RequestParam(name="id")String id){
        boolean b = orderService.deleteOrder(id);
        if(! b) {
            return ResponseType.create("fail", "删除订单失败!");
        }
        return ResponseType.create("删除订单成功!");
    }
    @RequestMapping("/toOrderDetial")
    @ResponseBody
    public ModelAndView toOrderDetial(@RequestParam(name="id")String id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/order/OrderDetail");
        modelAndView.addObject("orderId",id);
        return modelAndView;
    }
    @RequestMapping("/selectOrder")
    @ResponseBody
    public ResponseType selectOrder(@RequestParam(name="id")String id){
        OrderBo orderBo = orderService.selectOrderById(id);
        if(orderBo == null) {
            return ResponseType.create("fail", "订单号错误!");
        }
        return ResponseType.create(orderBo);
    }
}
